#include "QtWidgetsApplication.h"
#include <qboxlayout.h>
#include <qfiledialog.h>
#include <WinSock2.h>
#include <TidyUpIcon.h>


QtWidgetsApplication::QtWidgetsApplication(QWidget *parent)
    : QMainWindow(parent)
    , button(nullptr)
    , lineEdit(nullptr)
{
    ui.setupUi(this);
    setupUi();
    Connection();
 

}
void QtWidgetsApplication::setupUi()
{
 
	setWindowTitle("文件上传");
    setMinimumSize(400,550);
    setupCentralWidget();
}

void QtWidgetsApplication::setupCentralWidget()
{
   
    QWidget* centralWidget = new QWidget(this);
    setCentralWidget(centralWidget);
    // 在这里可以添加更多的UI组件和布局到centralWidget
    // 创建主布局
    QVBoxLayout* mainLayout = new QVBoxLayout(centralWidget);
    QGridLayout* layout = new QGridLayout();


    lineEdit = new QLineEdit();
    button = new QPushButton("上传文件");
    saveButton = new QPushButton("更改壁纸");
    setIcon = new QPushButton("整理图标");
    lineEdit->setPlaceholderText("请填写值");

    layout->addWidget(lineEdit, 0, 0);
    layout->addWidget(button, 0, 1);
    layout->addWidget(saveButton, 0, 2);
    layout->addWidget(setIcon, 0,3);


    mainLayout->addLayout(layout);
    //添加拉伸项以推送内容到顶部
    mainLayout->addStretch();
    
}
void QtWidgetsApplication::Connection()
{
    // 修改为使用指针并传递父窗口
    TidyUpIcon* tidyUpIcon = new TidyUpIcon(this);

    connect(
        button,
        &QPushButton::clicked,
        this,
        &QtWidgetsApplication::UploadFile
    );
    connect(
        saveButton,
        &QPushButton::clicked,
        this,
        &QtWidgetsApplication::SaveFile
    );
    connect(
        setIcon,
        &QPushButton::clicked,
        this,
        [tidyUpIcon]() {
            tidyUpIcon->show();
        }
    );
}
#pragma region 设置壁纸

void QtWidgetsApplication::SaveFile()
{
    bool v = setWindowsWallpaper(lineEdit->text());
}
/// <summary>
/// 设置壁纸
/// </summary>
/// <param name="imagePath"></param>
/// <returns></returns>
bool QtWidgetsApplication::setWindowsWallpaper(const QString& imagePath) {
    // 将QString路径转换为Windows API所需的格式
    QByteArray pathByteArray = imagePath.toLocal8Bit();
    const char* filePath = pathByteArray.constData();

    // 调用API设置壁纸
    BOOL result = SystemParametersInfoA(
        SPI_SETDESKWALLPAPER,
        0,
        (void*)filePath,
        SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE
    );
    return result;
}
void QtWidgetsApplication::UploadFile()
{
    QString fileName = QFileDialog::getOpenFileName(nullptr, "选择文件", "", "All Files (*.jpg *.png *.jpeg)");
    lineEdit->setText(fileName);

}
#pragma endregion
#pragma region 整理图标

#pragma endregion



QtWidgetsApplication::~QtWidgetsApplication()
{}

